import * as CryptoJS from "crypto-js";

export default {
  key: 'woaizhonghua',
  aesEncrypt: function (data: any) {
    if (typeof data === "object") {
      try {
        data = JSON.stringify(data);
      } catch (error) {
        console.log("加密错误");
      }
    }
    var key = CryptoJS.enc.Utf8.parse(this.key);
    // 加密
    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    };
    var encryptedData = CryptoJS.AES.encrypt(data, key, options);
    var encryptedBase64Str = encryptedData.toString();
    return encryptedBase64Str;
  },
  aesDecrypt: function (text: string) {
    var key = CryptoJS.enc.Utf8.parse(this.key);

    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    };

    var decryptedData = CryptoJS.AES.decrypt(text, key, options);

    var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    return decryptedStr;
  },
};
